<?php
/**
 * 产品管理
 *
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class ProductController extends BaseController {

	/**产品信息列表*/
	public function index(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		
		$DProduct = D('Admin/Product');
		$psize = I('get.psize', $this->m_psize,'intval');
		$product_name = I('get.product_name','','trim');
		$product_line = I('get.product_line');
		$wheres = array();
		$wheres['product.name'] = array('like','%'.$product_name.'%');
		if($product_line!=-1)
			$wheres['product.product_line_code'] = array('eq',$product_line);
		$MProduct = M('product');

		$cnt = $MProduct->join('LEFT JOIN product_line line on line.product_line_code=product.product_line_code')
						->join('LEFT JOIN user on product.manager_id = user.user_id')
						->where( $wheres )->count();

		$Page = new \Common\ORG\Page( $cnt, $psize );
		$product_list = $MProduct->field("product.*,user.nickname,line.name as line_name")
						->join('LEFT JOIN product_line line on line.product_line_code=product.product_line_code')
						->join('LEFT JOIN user on product.manager_id = user.user_id')
						->where($wheres)
						->order("product.create_time desc")
			            ->limit($Page->firstRow.','.$Page->listRows )
			            ->select();
		if( $product_list ){
			$res['rcode'] = 1;
			$res['data'] = $product_list;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "产品信息为空";
		}
		e_json( $res );
	}

	/*获取产品线数据*/
	public function getProductLine(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$productLine = M('product_line')->field("name,product_line_code")->select();
		if( $productLine ){
			$res['rcode'] = 1;
			$res['data'] = $productLine;
		}else{
			$res['msg'] = "产品线信息为空";
		}
		e_json( $res );
	}

	/*自动补全获取投资顾问信息*/
	public function getTeacherInfo(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$nickname = I('post.nickname','','trim');
		$map = array();
		if($nickname)	$map['nickname'] = array('like','%'.$nickname.'%');
		$map['is_deleted'] = array('neq',1);
		$teacherList = M('User')->where($map)->order('user_id desc')->select();
		if( $teacherList ){
			$res['rcode'] = 1;
			$res['data'] = $teacherList;
		}else{
			$res['msg'] = "投资顾问信息为空";
		}
		e_json( $res );
	}

	/*判断同一产品线下产品是否已经存在*/
	public function checkNameExist(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$product_line_code = I('post.product_line_code');
		$name = I('post.name');
		$product_id = I('post.product_id');
		$wheres = array();
		$wheres['product_line_code'] = array('eq',$product_line_code);
		$wheres['name'] = array('eq',$name);
		if($product_id)	//编辑页面功能
			$wheres['product_id'] = array('neq',$product_id);	
		$nameExist = M("product")->where($wheres)->find();
		if( $nameExist ){
			$res['rcode'] = 1;
		}else{
			$res['msg'] = "该产品名可以添加！";
		}
		e_json( $res );
	}

	/** 添加产品操作*/
	public function addProduct(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> '500 Internal Error');
		if (IS_POST) {
			$add_data = array();
			$add_data['name'] = I('post.name','','trim');
			$add_data['manager_id'] = I('post.manager_id','','trim');
			$add_data['risk_rank'] = I('post.risk_rank',false,'intval');
			$add_data['product_type'] = 0;//默认是全剧产品
			$add_data['sub_company'] = 1;//默认产品范围是全公司
			$add_data['product_line_code'] = I('post.product_line_code','','trim');
			$add_data['remark'] = I('post.remark','','trim');
			$MProduct = D('Product');
			$addres = $MProduct->add_Product($add_data);
			$res['rcode'] = $addres['r'];
			$res['data'] = $addres['d'];
			$res['msg'] = $addres['i'];
		}
		
		e_json( $res );
	}

	/* 根据用户id获取投顾老师信息*/
	public function getUserInfo(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$user_id = I('get.user_id',false,'intval');
		$wheres = array();
		$wheres['user_id'] = array('eq',$user_id);
		$userInfo = M("User")->field("user_id,nickname")->where($wheres)->find();
		if( $userInfo ){
			$res['rcode'] = 1;
			$res['data'] = $userInfo;
		}else{
			$res['msg'] = "用户信息为空";
		}
		e_json( $res );
	}

	/**产品编辑操作*/
	public function editProduct(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MProduct = D('Product');
		if (IS_GET){
			$product_id = I('get.product_id', 0, 'intval');
			$where = array();
			$where['p.product_id'] = array('eq', $product_id);
			$data = $MProduct->alias("p")
							 ->field("p.*,pl.name as line_name,u.nickname as manager_name")
							 ->join("LEFT JOIN product_line pl on  pl.product_line_code=p.product_line_code")
							 ->join("LEFT JOIN user u on u.user_id=p.manager_id")
							 ->where( $where )
							 ->find();
			if ($data){
				$res['rcode'] = 1;
				$res['msg'] = 'OK';
				$res['data'] = $data;
			}
			else{
				var_dump( mysql_error() );
				$res['msg'] = "产品信息不存在";
			}
		}
		else{
			$data = array();
			
			$data['product_id'] = I('post.product_id',false,'intval');
			$data['name'] = I('post.name','','trim');
			$data['remark'] = I('post.remark','','trim');
			$data['manager_id'] = I('post.manager_id','','intval');
			$data['risk_rank'] = I('post.risk_rank','','intval');
			$addres = $MProduct->update_Product($data);

			$res['rcode'] = $addres['r'];
			$res['data'] = $addres['d'];
			$res['msg'] = $addres['i'];
		}

		e_json( $res );
	}

	/** 产品删除操作*/
	public function delProduct(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$id = I('get.id',false,'intval');
		if( !$id ){
			$res['msg'] = '参数错误';
		}else{
			$MProduct = D('Product');
			$delres = $MProduct->delete_Product( $id );
			$res['rcode'] = $delres['r'];
			$res['msg'] = $delres['i'];
		}
		e_json( $res );
	}
}
